package org.eclipse.moquette.parser.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import org.eclipse.moquette.proto.messages.AbstractMessage;
import org.eclipse.moquette.proto.messages.PubAckMessage;

/**
 * Created by alex on 15-5-26.
 */
public class PubAckEncoder extends DemuxEncoder<PubAckMessage> {

    @Override
    protected void encode(ChannelHandlerContext ctx, PubAckMessage msg, ByteBuf out) {
        ByteBuf buff = ctx.alloc().buffer(4);

        try {
            buff.writeByte(AbstractMessage.PUBACK << 4);
            buff.writeBytes(Utils.encodeRemainingLength(2));
            buff.writeShort(msg.getMessageID());
            out.writeBytes(buff);
        } finally {
            buff.release();
        }
    }
}
